package com.hlx.controller.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hlx.entity.user.Ticket;
import com.hlx.service.user.CustomerService;
import com.hlx.service.admin.StationService;
import com.hlx.service.user.TicketService;
import com.hlx.service.admin.TrainNumberService;
import com.hlx.utils.Result;
import com.hlx.vo.TicketChangeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/ticketChange")
public class TicketChangeVoController {
    @Autowired
    private TicketService ticketService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private StationService stationService;
    @Autowired
    private TrainNumberService trainNumberService;

    @GetMapping("/getAll/{userId}/{pageSize}/{currentPage}")
    public Result<Page> getAllTicketChangeByUserId(@PathVariable Integer userId,@PathVariable Integer pageSize,@PathVariable Integer currentPage){
//        获取此用户下的所有用户
        List<Integer> customerId = customerService.getAllCustomerIdByCustomerUser(userId);
//        获取所有车票信息
        Page page = ticketService.getTicketByCustomerId(customerId,pageSize,currentPage);
        List<TicketChangeVo> ticketChangeVos = new ArrayList<>();
        Iterator<Ticket> it = page.getRecords().iterator();
        while (it.hasNext()){
            TicketChangeVo tcv = new TicketChangeVo();
            Ticket t = it.next();
            tcv.setId(t.getId());
            tcv.setTrainId(t.getTrainId());
            tcv.setStartId(t.getStartId());
            tcv.setArriveId(t.getArriveId());
            tcv.setPrice(t.getPrice());
            tcv.setSeatType(t.getSeatType());
            tcv.setDiscountType(t.getDiscountType());
            tcv.setPersonId(t.getPersonId());
            tcv.setStatus(t.getStatus());
            tcv.setDate(t.getDate());
            tcv.setTrainNumber(trainNumberService.getById(tcv.getTrainId()).getTrainNumberName());
            tcv.setStartStation(stationService.getById(tcv.getStartId()).getStationName());
            tcv.setArriveStation(stationService.getById(tcv.getArriveId()).getStationName());
            tcv.setCustomerName(customerService.getById(tcv.getPersonId()).getCustomerName());
            ticketChangeVos.add(tcv);
        }
        if(ticketChangeVos.isEmpty()){
            return Result.error("查询失败");
        }
        page.setRecords(ticketChangeVos);
        return Result.success(page);
    }

    @GetMapping("/searchTicketByName/{customerName}/{pageSize}/{currentPage}")
    public Result<Page> searchTicketChangeByName(@PathVariable String customerName,@PathVariable Integer pageSize,@PathVariable Integer currentPage){
//        获取此用户下的所有用户
        List<Integer> customerId = customerService.getAllCustomerByName(customerName);
        if(customerId.isEmpty()){
            return Result.error("查询失败");
        }
//        获取所有车票信息
        Page page = ticketService.getTicketByCustomerId(customerId,pageSize,currentPage);
        
        List<TicketChangeVo> ticketChangeVos = new ArrayList<>();
        Iterator<Ticket> it = page.getRecords().iterator();
        while (it.hasNext()){
            TicketChangeVo tcv = new TicketChangeVo();
            Ticket t = it.next();
            tcv.setId(t.getId());
            tcv.setTrainId(t.getTrainId());
            tcv.setStartId(t.getStartId());
            tcv.setArriveId(t.getArriveId());
            tcv.setPrice(t.getPrice());
            tcv.setSeatType(t.getSeatType());
            tcv.setDiscountType(t.getDiscountType());
            tcv.setPersonId(t.getPersonId());
            tcv.setStatus(t.getStatus());
            tcv.setDate(t.getDate());
            tcv.setTrainNumber(trainNumberService.getById(tcv.getTrainId()).getTrainNumberName());
            tcv.setStartStation(stationService.getById(tcv.getStartId()).getStationName());
            tcv.setArriveStation(stationService.getById(tcv.getArriveId()).getStationName());
            tcv.setCustomerName(customerService.getById(tcv.getPersonId()).getCustomerName());
            ticketChangeVos.add(tcv);
        }
        if(ticketChangeVos.isEmpty()){
            return Result.error("查询失败");
        }
        page.setRecords(ticketChangeVos);
        return Result.success(page);
    }
}
